using System;
using Yoozoo.Gameplay.RTS;

namespace Yoozoo.Gameplay
{
    public class TimeUtils
    {
        public static long GetServerTimeMs()
        {
#if UNITY_EDITOR
            if (RtsArenaManager.IsTimeDebug)
            {
                return RtsArenaManager.Instance.GetServerTimeMs();
            }
#endif
            if (clientStartTick == 0)
            {
                clientStartTick = Environment.TickCount;
            }
            //return NetworkManager.GetInstance().NetworkPing.ServerTime;
            //TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Environment.TickCount - clientStartTick + serverTime; //TODO
        }

        public static long GetServerTime()
        {
            return serverTime + Environment.TickCount - clientStartTick;
        }
        // 获得客户端启动以来的事件
        public static long GetClientTickTimeMS()
        {
#if UNITY_EDITOR
            if (RtsArenaManager.IsTimeDebug)
            {
                return RtsArenaManager.Instance.GetClientTimeMs();
            }
#endif
            return Environment.TickCount - clientStartTick;
        }

        public static float GetClientTickTime()
        {
#if UNITY_EDITOR
            if (RtsArenaManager.IsTimeDebug)
            {
                return RtsArenaManager.Instance.GetClientTimeMs()/1000f;
            }
#endif
            return (Environment.TickCount - clientStartTick)/1000f;
        }
        public static long clientStartTick;
        private static long serverTime;

        public static void UpdateServerTimeStamp(long timeStamp)
        {
            serverTime = timeStamp;
            clientStartTick = Environment.TickCount;
        }
        
        private static readonly DateTime UtcTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);

        public static long GetTimeStampYMDHM(int year,int month, int day, int hour, int minutes)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            var dataTime = new DateTime(year, month, day, hour, minutes, 0, 0);
            TimeSpan ts = dataTime - dtStart;
            return Convert.ToInt64(ts.TotalMilliseconds);
        }

        public static long GetUMTTimeStampYMDHM(int year, int month, int day, int hour, int minutes)
        {
            var dataTime = new DateTime(year, month, day, hour, minutes, 0, 0);
            TimeSpan ts = dataTime - UtcTime;
            return Convert.ToInt64(ts.TotalMilliseconds);
        }
        public static string MillisecondsToTime(long milliseconds)
        {
            TimeSpan time = TimeSpan.FromMilliseconds(milliseconds);
            return time.ToString(@"hh\:mm\:ss");
        }
        
        /// <summary>
        /// 根据Utc时间获取本地时间的DataTime
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="hour"></param>
        /// <param name="minutes"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        public static DateTime GetLocalDataTimeByUtcTime(int year, int month, int day, int hour, int minutes, int second)
        {
            var dataTime = new DateTime(year, month, day, hour, minutes, second, 0);
            TimeZoneInfo localTime = TimeZoneInfo.Local;
            DateTime localDataTime = TimeZoneInfo.ConvertTimeFromUtc(dataTime, localTime );
            return localDataTime;
        }
		
		public static long GetUTC2LocalDeltaMilliseconds()
        {
            return Convert.ToInt64((DateTime.Now - DateTime.UtcNow).TotalMilliseconds);
        }
        
        public static long GetUTCTimeStampYMDHM(int year, int month, int day, int hour, int minutes, bool isUTC)
        {
            var dataTime = new DateTime(year, month, day, hour, minutes, 0, 0);
            
            TimeSpan ts;
            if (isUTC)
            {
                ts = dataTime - UtcTime;
            }
            else
            {
                ts = dataTime + (DateTime.UtcNow - DateTime.Now) - UtcTime;
            }

            return Convert.ToInt64(ts.TotalMilliseconds);
        }
    }
}
